$(document).ready(function() {
	getRewardType();
	getRewardLevel();
	getProcess();
	
	//填充当前时间
	$("#happenTime").val(new Date().Format("yyyy-MM-dd"));
	//填充任务编号
	$("#applyCode").val(getTimeBusiflow());
});

//获取奖惩类型
function getRewardType(){
	$.ajax({ 
	 	url: ctp + "/rewardController/getRewardType.shtml",
		type : 'POST',
		dataType: "json", 
		success : function(result) {
			if(result != null){
				var list = result.list;
				for(var i = 0; i < list.length; i++){
					var g = list[i];
					$("#rewardType").append('<option value="'+g["REC_KEY"]+'">'+g["REC_VALUE"]+'</option>');
				}
			}
		}
	});
}

//获取奖惩等级
function getRewardLevel(){
	$.ajax({ 
	 	url: ctp + "/rewardController/getRewardLevel.shtml",
		type : 'POST',
		dataType: "json", 
		success : function(result) {
			if(result != null){
				var list = result.list;
				for(var i = 0; i < list.length; i++){
					var g = list[i];
					if(i == list.length - 1){
						$("#rewardLevel").append('<option value="'+g["REC_KEY"]+'" selected>'+g["REC_VALUE"]+'</option>');
					}else{
						$("#rewardLevel").append('<option value="'+g["REC_KEY"]+'">'+g["REC_VALUE"]+'</option>');
					}
					
				}
			}
		}
	});
}

//获取审批流程
function getProcess(){
	$.ajax({ 
	 	url: ctp + "/processController/getProcessList.shtml",
	 	data: {"processType": "rewardApply"},
		type : 'POST',
		dataType: "json", 
		success : function(result) {
			if(result != null){
				var list = result.list;
				for(var i = 0; i < list.length; i++){
					var g = list[i];
					$("#processId").append('<option value="'+g["REC_ID"]+'">'+g["NAME"]+'</option>');
				}
			}
		}
	});
}

function saveRewardApply(){
	if (!Validator.Validate(document.getElementById("form1"), 1)) {
		return;
	}
	
	$.ajax({ 
	 	url: ctp + "/rewardController/saveRewardApply.shtml",
	 	data: $('#form1').serialize(),
		type : 'POST',
		success : function(result) {
			if(result=="") {
				jalert9("奖惩申请提交成功！");
				$("#rewardType").val('');
				$("#rewardLevel").val('4');
				$("#rewardMoney").val('0');
				$("#projectName").val('');
				$("#projectId").val('');
				$("#workerName").val('');
				$("#workerId").val('');
				$("#rewardDesc").val('');
				$("#rewardResult").val('');
			}else{
				jalert(result);
			}
		}
	});
	
}

//获取工人
function getWorker(){
	var projectId = $("#projectId").val();
	if(projectId == ""){
		jalert("请先选择工人所属工程项目");
		return;
	}
	layui.layer.open({
        type: 2,
        area: ['800px', '470px'],
        title: "添加工人",
        fixed: false,
        maxmin: false,
        content: ctp + "/rewardController/pageWorker.shtml?projectId=" + projectId
    });
}

//获取工程项目
function getProject(){
	layui.layer.open({
        type: 2,
        area: ['800px', '470px'],
        title: "添加工程项目",
        fixed: false,
        maxmin: false,
        content: ctp + "/rewardController/pageProject.shtml"
    });
}


function ClearForm(id) {
    var objId = document.getElementById(id);
    if (objId == undefined) {
        return;
    }
    for (var i = 0; i < objId.elements.length; i++) {
        if (objId.elements[i].type == "text") {
            objId.elements[i].value = "";
        }
        else if (objId.elements[i].type == "password") {
            objId.elements[i].value = "";
        }
        else if (objId.elements[i].type == "radio") {
            objId.elements[i].checked = false;
        }
        else if (objId.elements[i].type == "checkbox") {
            objId.elements[i].checked = false;
        }
        else if (objId.elements[i].type == "select-one") {
            objId.elements[i].options[0].selected = true;
        }
        else if (objId.elements[i].type == "select-multiple") {
            for (var j = 0; j < objId.elements[i].options.length; j++) {
                objId.elements[i].options[j].selected = false;
            }
        }
        else if (objId.elements[i].type == "textarea") {
            objId.elements[i].value = "";
        }
    }
}